package com.sy.pet.service;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sy.pet.commom.Fileutils;
import com.sy.pet.commom.enmu.AuditStatus;
import com.sy.pet.dto.PageDTO;
import com.sy.pet.dto.PetAdoptionQueryDTO;
import com.sy.pet.dto.UserAuthenticationDTO;
import com.sy.pet.entity.PetInfoTag;
import com.sy.pet.entity.User;
import com.sy.pet.entity.UserAuthentication;
import com.sy.pet.mapper.UserAuthenticationMapper;
import com.sy.pet.mapper.UserMapper;
import com.sy.pet.vo.PetAdoptionRecordVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * @Author: liangSY
 * @Date: 2024/9/16
 * @ClassName: UserAuthenticationService
 * @Description: UserAuthenticationService描述
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class UserAuthenticationService extends ServiceImpl<UserAuthenticationMapper, UserAuthentication> {
    @Value("${pet.file-root-path}")
    private String fileRootPath;
    @Value("${pet.file-request-url}")
    private String fileRequestUrl;
    @Autowired
    UserMapper userMapper;

    public void add(UserAuthenticationDTO dto) {
        String userId = StpUtil.getLoginIdAsString();
        String idCardFrontImage = Fileutils.uploadFile(fileRootPath, dto.getIdCardFrontImage(), "id-card");
        String idCardBackImage = Fileutils.uploadFile(fileRootPath, dto.getIdCardBackImage(), "id-card");
        baseMapper.insert(UserAuthentication.builder()
                .userId(userId)
                .realName(dto.getRealName())
                .sex(dto.getSex())
                .status(AuditStatus.AUDIT.getCode())
                .idCardNumber(dto.getIdCardNumber())
                .idCardFrontImage(idCardFrontImage)
                .idCardBackImage(idCardBackImage)
                .createTime(LocalDateTime.now())
                .build());
    }

    public void audit(String id,AuditStatus status) {
        UserAuthentication userAuthentication = baseMapper.selectById(id);
        userAuthentication.setStatus(status.getCode());
        baseMapper.updateById(userAuthentication);
        if(status == AuditStatus.PASS){
            User user = userMapper.selectById(userAuthentication.getUserId());
            user.setRealName(userAuthentication.getRealName());
            user.setSex(userAuthentication.getSex());
            userMapper.updateById(user);
        }
    }

    public IPage<UserAuthentication> getPageData(PageDTO dto){
        Page<UserAuthentication> page = new Page(dto.getPage(), dto.getSize());
        LambdaQueryWrapper<UserAuthentication> queryWrapper = Wrappers.lambdaQuery();
        IPage<UserAuthentication> pageData = baseMapper.selectPage(page, queryWrapper);
        pageData.getRecords().stream().forEach(item -> {
            item.setIdCardFrontImage(fileRequestUrl+item.getIdCardFrontImage());
            item.setIdCardBackImage(fileRequestUrl+item.getIdCardBackImage());
        });
        return pageData;
    }

}
